﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rotempco.ORM.Generator.Objects;
using System.Data.SqlClient;
using Rotempco.ORM.Generator.Enums;

namespace Rotempco.ORM.Generator.Logic
{
    public class ColumnSettingsLogic
    {
        public static List<DatabaseColumn> GetColumnsWithSettingsFromQuery(RotempcoSettings projectSettings, string sqlQuery, SqlParameter[] sqlParameters)
        {
            bool? databaseUseIntegrated = projectSettings.GetBoolValue("@DatabaseUseIntegrated");

            List<DatabaseColumn> databaseColumns = DatabaseInfoRetrieverLogic.GetColumnsFromQuery(
                projectSettings.GetValue("@DatabaseAddress"),
                projectSettings.GetValue("@DatabaseName"),
                sqlQuery,
                sqlParameters,
                databaseUseIntegrated.HasValue ? databaseUseIntegrated.Value : false,
                projectSettings.GetValue("@DatabaseLogin"),
                projectSettings.GetValue("@DatabasePassword"));




            return databaseColumns;
        }


        public static List<DatabaseColumn> GetColumnsWithSettings(RotempcoSettings projectSettings, string tableName)
        {
            bool? databaseUseIntegrated = projectSettings.GetBoolValue("@DatabaseUseIntegrated");

            List<DatabaseColumn> databaseColumns = DatabaseInfoRetrieverLogic.GetColumns(
                projectSettings.GetValue("@DatabaseAddress"),
                projectSettings.GetValue("@DatabaseName"),
                tableName,
                databaseUseIntegrated.HasValue ? databaseUseIntegrated.Value : false,
                projectSettings.GetValue("@DatabaseLogin"),
                projectSettings.GetValue("@DatabasePassword"));

            foreach (DatabaseColumn databaseColumn in databaseColumns)
            {
                string enumDefinitionName = projectSettings.GetValue("@EnumDefinitions_" + tableName + "_" + databaseColumn.Name);
                string enumDefinitionNamespace = projectSettings.GetValue("@EnumDefinitions_Namespace_" + tableName + "_" + databaseColumn.Name);

                if (!string.IsNullOrEmpty(enumDefinitionName) && !string.IsNullOrEmpty(enumDefinitionNamespace))
                {
                    databaseColumn.IsEnum = true;
                    databaseColumn.EnumName = enumDefinitionName;
                    databaseColumn.EnumNamespace = enumDefinitionNamespace;
                }

                string serializableObjectDefinitionName = projectSettings.GetValue("@SerializableObjectDefinitions_" + tableName + "_" + databaseColumn.Name);
                string serializableObjectDefinitionNamespace = projectSettings.GetValue("@SerializableObjectDefinitions_Namespace_" + tableName + "_" + databaseColumn.Name);

                if (!string.IsNullOrEmpty(serializableObjectDefinitionName) && !string.IsNullOrEmpty(serializableObjectDefinitionNamespace))
                {
                    databaseColumn.IsSerializable = true;
                    databaseColumn.SerializableName = serializableObjectDefinitionName;
                    databaseColumn.SerializableNamespace = serializableObjectDefinitionNamespace;
                }

                bool? deletedFlag = projectSettings.GetBoolValue("@DeletedFlag_" + tableName + "_" + databaseColumn.Name);
                string encryptedFlag = projectSettings.GetValue("@EncryptedFlag_" + tableName + "_" + databaseColumn.Name);
                string encryptedColumnType = projectSettings.GetValue("@EncryptedDataType_" + tableName + "_" + databaseColumn.Name);

                // From old encryption scheme:
                if (encryptedFlag == "False")
                    encryptedFlag = "ClearText";
                else if (encryptedFlag == "True")
                    encryptedFlag = "Symmetric";

                if (!string.IsNullOrWhiteSpace(encryptedFlag) && encryptedFlag != "ClearText" )
                    databaseColumn.EncryptionType = (EncryptionTypeEnum)Enum.Parse(typeof(EncryptionTypeEnum), encryptedFlag);
                else
                    databaseColumn.EncryptionType = EncryptionTypeEnum.ClearText;

                databaseColumn.DeletedFlag = (deletedFlag.HasValue && deletedFlag.Value == true);
                databaseColumn.EncryptedDataType = encryptedColumnType;
            }


            return databaseColumns;
        }
    }
}
